package de.clapp.musicbox.viewpages;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import de.clapp.CLAppModel;
import de.clapp.R;
import de.clapp.Settings;
import de.clapp.backend.network.ClientRequest;
import de.clapp.backend.network.TCPSocket;
import de.clapp.backend.requests.LastPlayedSongsRequest;
import de.clapp.data.Club;
import de.clapp.data.MusicItem;
import de.clapp.musicbox.MusicFragment;
import de.clapp.musicbox.MusicModel;
import de.clapp.musicbox.MusicProgressThread;
import de.clapp.musicbox.listAdapter.CurrentlyPlayingListAdapter;
import de.clapp.musicbox.loader.MusicPlayedSongsLoader;
import de.clapp.toolkit.ImageManager;
import de.clapp.toolkit.OneSideSwipeableViewPager;

@SuppressLint("ValidFragment")
public class CurrentlyPlayingMusicFragmentList extends ListFragment implements LoaderCallbacks<ArrayList<MusicItem>>
{
    private OneSideSwipeableViewPager myViewPager;

    private CurrentlyPlayingListAdapter adapter;

    private Thread songListener;

    private MusicItem currentSong;

    private TextView top, bottom;

    private ProgressBar bar;

    private ImageView image;

    private String title;

    public ListView mList;

    boolean mListShown;

    private LinearLayout layout;

    View mProgressContainer;

    View mListContainer;
    
    private MusicProgressThread progressThread;

    public CurrentlyPlayingMusicFragmentList()
    {
        // super("Aktuell");
        this.title = "Aktuell";
    }

    public String getTitle()
    {
        return this.title;
    }

    public void setListShown(boolean shown, boolean animate)
    {
        if (mListShown == shown)
        {
            return;
        }
        mListShown = shown;
        if (shown)
        {
            if (animate)
            {
                mProgressContainer.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_out));
                mListContainer.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in));
            }
            mProgressContainer.setVisibility(View.GONE);
            mListContainer.setVisibility(View.VISIBLE);
        }
        else
        {
            if (animate)
            {
                mProgressContainer.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in));
                mListContainer.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_out));
            }
            mProgressContainer.setVisibility(View.VISIBLE);
            mListContainer.setVisibility(View.INVISIBLE);
        }
    }

    public void setListShown(boolean shown)
    {
        setListShown(shown, true);
    }

    public void setListShownNoAnimation(boolean shown)
    {
        setListShown(shown, false);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        setHasOptionsMenu(false);

        int INTERNAL_EMPTY_ID = 0x00ff0001;
        View view = inflater.inflate(R.layout.music_currently_playing, container, false);
        (view.findViewById(R.id.internalEmpty)).setId(INTERNAL_EMPTY_ID);
        mList = (ListView) view.findViewById(android.R.id.list);
        mListContainer = view.findViewById(R.id.listContainer);
        mProgressContainer = view.findViewById(R.id.progressContainer);
        mListShown = true;

        this.adapter = new CurrentlyPlayingListAdapter(inflater.getContext(), this);
        this.setListAdapter(this.adapter);

        this.top = (TextView) view.findViewById(R.id.music_entry_title);
        this.bottom = (TextView) view.findViewById(R.id.music_entry_artist);
        this.image = (ImageView) view.findViewById(R.id.music_entry_image);
        this.bar = (ProgressBar) view.findViewById(R.id.music_entry_voteBar);
        this.layout = (LinearLayout) view.findViewById(R.id.thumbnail);

        Button button = (Button) view.findViewById(R.id.btn_vote);
        button.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View arg0)
            {
                MusicFragment.myViewPager.setCurrentItem(MusicFragment.myViewPager.getCurrentItem() + 1, true);
            }

        });

        return view;
    }

    @Override
    public void onResume()
    {
        super.onResume();
        this.songListener = new Thread();
        this.songListener.start();

        this.getLoaderManager().initLoader(Settings.LOADERNUMBER++, null, this).forceLoad();
    }

    @Override
    public void onPause()
    {
        super.onPause();
        this.songListener.interrupt();
    }

    @Override
    public Loader<ArrayList<MusicItem>> onCreateLoader(int arg0, Bundle arg1)
    {
        return new MusicPlayedSongsLoader(this.getActivity());
    }

    private void setUIForCurrentSong(int progress, int length)
    {
        this.top.setText(this.currentSong.getTitel());
        this.bottom.setText(this.currentSong.getInterpret() + " - " + this.currentSong.getAlbum());
        if (length != 0)
        {
            this.bar.setMax(length);
            this.bar.setProgress(progress);
            this.bar.refreshDrawableState();

            if(this.progressThread == null)
            {
                this.progressThread = new MusicProgressThread(bar);
                this.progressThread.start();
            }
        }
        String tag = "MusicImages\\" + this.currentSong.getInterpret() + ".png";
        this.image.setTag(tag);
        Club club = CLAppModel.getInstance().getCurrentClub();
        ImageManager.getInstance().loadBitmap(tag, image, 50, 50, club.getPort(), club.getIpAddress());
    }

    public void hideGUI(boolean value)
    {
        if (value)
        {
            this.top.setVisibility(View.GONE);
            this.bottom.setVisibility(View.GONE);
            this.bar.setVisibility(View.GONE);
            this.image.setVisibility(View.GONE);
            this.layout.setVisibility(View.GONE);
        }
        else
        {
            this.top.setVisibility(View.VISIBLE);
            this.bottom.setVisibility(View.VISIBLE);
            this.bar.setVisibility(View.VISIBLE);
            this.image.setVisibility(View.VISIBLE);
            this.layout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onLoadFinished(Loader<ArrayList<MusicItem>> loader, ArrayList<MusicItem> list)
    {
        try
        {
            if (list.size() > 0)
            {
                MusicPlayedSongsLoader myLoader = (MusicPlayedSongsLoader) loader;
                int progress = myLoader.getCurrentPosition();
                this.hideGUI(false);
                // das erste Element ist das derzeit aktuell abgespielte
                this.currentSong = list.get(0);
                list.remove(0);
                this.setUIForCurrentSong(progress, this.currentSong.getLength());

                MusicModel.getI().setAlreadyPlayedSongs(list);
            }
            else
            {
                this.hideGUI(true);
//                Toast.makeText(this.getActivity(), "Es wurde noch kein Lied in diesem Club abgespielt.", Toast.LENGTH_LONG).show();
            }
        }
        catch (Exception e)
        {
            Toast.makeText(this.getActivity(), "Fehler beim Laden des aktuellen Songs - Bitte versuchen Sie es in einem Moment erneut.", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onLoaderReset(Loader<ArrayList<MusicItem>> arg0)
    {
    }
}
